package  com.earstep.view
{
	import com.earstep.component.PopupWindow;
	import com.earstep.component.PopupWindowGroup;
	import com.erastep.event.CommonEvent;
	import com.erastep.mvc.ifc.INotification;
	import com.erastep.mvc.mediator.Mediator;
	import com.erastep.util.GUID;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.containers.ViewStack;
	import mx.core.DragSource;
	import mx.core.IVisualElement;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	import spark.filters.DropShadowFilter;
	
	/**
	 * @depart 宁波维涛
	 * @author Jarvis 
	 * @E-mail: shaomj@victopsoft.com
	 * @version 1.0.1 
	 * 创建时间：2011-12-5 下午02:07:03 * 
	 */
	public class PopupWindowMediator extends Mediator
	{
		
		public static const NAME:String = "popupWindowMediator";
		
		private var vs:ViewStack;
		
		private var popObject:Object;
		
		private var currentX:Number = 0,currentY:Number = 0;
		
		public function PopupWindowMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			vs = viewComponent["mainView"] as ViewStack;
			popObject = new Object();
		}
		
		/**
		 * 弹出一个新的窗口
		 * 参数结构 {title:String,view:displayObject,width:int,height:int,closeFun:Function,windowId:String,align:String,textAlign:String,modal:Boolean,dragSize:Boolean}
		 * align  center 代表居中 默认状态  left 窗口右上角
		 * modal  true 模态窗口 默认状态  false 没有覆盖层
		 * textAlign  center 标题居中 left 标题居左默认  right 标题居右
		 * */
		public function respondToPopupWindow(note:INotification):void{
			var fromId:String = note.getFromId();
			var mainView:Canvas;
			for(var i:uint = 0;i < vs.numChildren;i++){
				if(vs.getChildAt(i).name == fromId){
					mainView = vs.getChildAt(i) as Canvas;
					break;
				}
			}
			
			var body:Object = note.getBody();
			var titleWindow:PopupWindow = new PopupWindow();
			var popGroup:PopupWindowGroup = new PopupWindowGroup();
			popGroup.percentHeight = 100;
			popGroup.percentWidth = 100;
			mainView.addChild(popGroup);
			 
			if(body.hasOwnProperty("width"))
				titleWindow.width = body.width;
			if(body.hasOwnProperty("height"))
				titleWindow.height = body.height+28;
			if(body.hasOwnProperty("title"))
				titleWindow.titleText.text = body.title;
			if(body.hasOwnProperty("dragSize") && !Boolean(body.dragSize)){
				titleWindow.dragSize.visible = false;
			}
			
			
			titleWindow.y = (mainView.height-titleWindow.height)/2;
			titleWindow.x = (mainView.width-titleWindow.width)/2;
			if(!body.hasOwnProperty("align") ||　body.align == "left"){
				titleWindow.y = 0;
				titleWindow.x = 0;
			}
			titleWindow.dragBar.addEventListener(MouseEvent.MOUSE_DOWN,dragStart);
			popGroup.addEventListener(DragEvent.DRAG_ENTER,startGroup);
			popGroup.addEventListener(DragEvent.DRAG_DROP,stopGroup);
			if(body.hasOwnProperty("maxWindow")){
				if(body.maxWindow){
					titleWindow.top = 15;
					titleWindow.left = 10;
					titleWindow.right = 10;
					titleWindow.bottom = 10;
					titleWindow.dragBar.removeEventListener(MouseEvent.MOUSE_DOWN,dragStart);
					popGroup.removeEventListener(DragEvent.DRAG_ENTER,startGroup);
					popGroup.removeEventListener(DragEvent.DRAG_DROP,stopGroup);
				}
			}
			
			
			if(popObject.hasOwnProperty(body.windowId)){
				mainView.removeChild(popObject[body.windowId] as PopupWindowGroup);
			}
			if(body.hasOwnProperty("windowId")){
				titleWindow.id = body.windowId;
		    }else{
				titleWindow.id = GUID.create();
			}
			if(body.hasOwnProperty("closeFun")){
				titleWindow.addEventListener("closeWindow",(body.closeFun as Function));
			}else{
				titleWindow.addEventListener("closeWindow",closeWindow);
			}
			if(body.hasOwnProperty("textAlign") &&　body.textAlign == "center" ){
				titleWindow.titleText.setStyle("textAlign","center");
			}else if(body.hasOwnProperty("textAlign") &&　body.textAlign == "left"){
				titleWindow.titleText.setStyle("textAlign","left");
			}else if(body.hasOwnProperty("textAlign") &&　body.textAlign == "right"){
				titleWindow.titleText.setStyle("textAlign","right");
			}
			titleWindow.titleText.validateNow();
			popGroup.modalCalvas.visible = true;
			if(body.hasOwnProperty("modal") && body.modal == false){
				popGroup.modalCalvas.visible = false;
			}else{
				popGroup.showBackEffect.play();
			}
			
			titleWindow.mainViewElement.addElement(body.view as IVisualElement);
			
			popGroup.titleWindow = titleWindow;
			popGroup.addElement(titleWindow);
			popGroup.clipAndEnableScrolling = true;
			popObject[body.windowId] = popGroup;
			
			var arrFilters:Array = new Array();
			var filters:DropShadowFilter = new DropShadowFilter(6,45,0X333333,0.35,7,7,8);
			arrFilters.push(filters);
			popGroup.filters = arrFilters;
			
			var mm:Function = function(e:MouseEvent):void{
				var currentX:Number = titleWindow.x;
				var currentY:Number = titleWindow.y;
				var width:Number = popGroup.mouseX - currentX;
				var height:Number = popGroup.mouseY - currentY;
				if(width > 200){
					titleWindow.width = width;
				}
				if(height > 200){
					titleWindow.height = height;
				}
			};
			
			titleWindow.dragSize.addEventListener(MouseEvent.MOUSE_DOWN,function(e:MouseEvent):void{
				titleWindow.stateDrag = true;
				popGroup.addEventListener(MouseEvent.MOUSE_MOVE,mm);
			});
			
			popGroup.addEventListener(MouseEvent.MOUSE_UP,function(e:MouseEvent):void{
				titleWindow.stateDrag = false;
				popGroup.removeEventListener(MouseEvent.MOUSE_MOVE,mm);
			});
		}
		
		
		/**
		 * 关闭一个窗口
		 * */
		public function respondToRemovePopupWindow(note:INotification):void{
			if(note.getBody()){
				var fromId:String = note.getFromId();
				var mainView:Canvas;
				for(var i:uint = 0;i < vs.numChildren;i++){
					if(vs.getChildAt(i).name == fromId){
						mainView = vs.getChildAt(i) as Canvas;
						break;
					}
				}
				var id:String = note.getBody().toString();
				if(popObject.hasOwnProperty(id)){
					mainView.removeChild(popObject[id] as PopupWindowGroup);
					delete popObject[id];
				}
			}
		}
		
		private function stopGroup(e:DragEvent):void{
			var dragData:Object= e.dragSource.dataForFormat("popupwindow");
			var initiator:UIComponent = e.dragInitiator as UIComponent;
			if(currentX != 0 && currentY != 0){
				var tempX:Number =  (e.currentTarget as PopupWindowGroup).mouseX - currentX;
				var tempY:Number =  (e.currentTarget as PopupWindowGroup).mouseY - currentY;
				PopupWindow(e.dragInitiator).x = PopupWindow(e.dragInitiator).x+tempX;
				PopupWindow(e.dragInitiator).y = PopupWindow(e.dragInitiator).y+tempY;
			}
		}
		
		private function startGroup(e:DragEvent):void{
			if(e.dragSource.hasFormat("popupwindow")){
				DragManager.acceptDragDrop(e.target as UIComponent);
			}
		}
		
		private function dragStart(e:MouseEvent):void{
			var source:DragSource = new DragSource();
			var itemData:Object = e.currentTarget.parent;   
			source.addData(itemData,"popupwindow");
			DragManager.doDrag(itemData as UIComponent,source,e);
			currentX = (itemData.parent as PopupWindowGroup).mouseX;
			currentY = (itemData.parent as PopupWindowGroup).mouseY;
		}
		private function closeWindow(e:CommonEvent):void{
		    var p:PopupWindow =  e.currentTarget as PopupWindow;
			if(popObject.hasOwnProperty(p.id)){
				var pwg:PopupWindowGroup = popObject[p.id] as PopupWindowGroup;
				if(pwg){
					var box:Canvas = pwg.parent as Canvas;
					box.removeChild(popObject[p.id] as PopupWindowGroup);
					delete popObject[p.id];
				}
			}
		}
	}
}